import request from '@/common/config/api.js'

export default {
	namespaced: true, //模块与文件同名
	state: {
		dataDictionary: [], //数据字典

		isLogin: false,
		userInfo: {},
		edition: {}, //版本信息
	},
	mutations: {
		//会员登录状态
		$SET_USER_STATE(state, data) {
			state.isLogin = data;
		},
		//会员信息
		$SET_USER_INFO(state, data) {
			state.userInfo = data;
		},

		//版本信息
		$EDITION(state, data) {
			state.edition = data;
		},
		//更新用户信息时间
		$SET_nowTime(state, data) {
			state.nowTime = data;
		},

		//数据字典
		SET_DataDictionary: (state, data) => {
			state.dataDictionary = data
		},
	},
	actions: {
		//初始化
		$INIT(context) {
			let token = uni.getStorageSync('userToken');
			if (token) {
				context.dispatch('$GET_USER_INFO') // 获取会员信息

				context.dispatch('getDataDictionary') // 获取数据字典
				
				context.dispatch('GetPageList') // 获取审批总进度
				
				context.commit('$SET_USER_STATE', true) //登录状态
			} else {
				context.commit('$SET_USER_STATE', false) //登录状态
			}
		},

		//获取会员信息
		$GET_USER_INFO(context) {
			return request.http.get('/api/Users/GetInfo', {}).then(res => {
				context.commit('$SET_USER_INFO', res)
				context.commit('$SET_nowTime', Date.parse(new Date()))
			})
		},

		// 获取数据字典(每次登录状态下刷新页面如果没有缓存则会执行一次)
		getDataDictionary(context) {
			const params = {
				"currentPage": 1,
				"pageSize": 10000
			}
			return request.http.post('/api/DataDic_Detail/GetPageList', params).then(res => {
				context.commit('SET_DataDictionary', res.list)
				uni.setStorageSync('dataDictionary', res.list)
				
				context.dispatch('getNextXZQH') // 获取行政区划
			})
		},
		
		// 获取审批总进度
		GetPageList(context) {
			const params = {
				"currentPage": 1,
				"pageSize": 10000
			}
			return request.http.post('/api/SPSZ/GetPageList', params).then(res => {
				uni.setStorageSync('SPSZPageList', res.list)
			})
		},
		
		// 获取行政区划
		getNextXZQH(context) {
			let arr = []
			let data = uni.getStorageSync('dataDictionary')
			arr = data.filter((item)=>{
				return item.dicCode == 'XZQH'
			})
			
			let xzrhmrdm = arr //行政区划
			let xzqhdm = ''
			xzrhmrdm && xzrhmrdm.forEach(item => {
				if (item.value == 'XZQHMRDM') xzqhdm = item.key
			})
			
			const params = {
				xzqhdm: xzqhdm
			}
			
			return request.http.post('/api/XZQH/GetNextXZQH', params).then(res => {
				uni.setStorageSync('XZQH', res.xzqh)
			})
		},
		
		//退出登录
		$LOGOUT(context) {
			context.commit('$SET_USER_STATE', false);
			context.commit('$SET_USER_INFO', null);
			uni.removeStorageSync('userToken');

			uni.reLaunch({
				url: `/pages/login/login`,
			})


		},

		// APP更新
		appUpdate(ctx) {
			return request.http.get('/user/appUpdateAddr.html', {}).then(res => {

				// console.log(res)

				// 系统平台
				let OS = uni.getSystemInfoSync().platform
				// 服务端 安卓版本
				let android_version = null
				// 服务端 安卓包链接
				let android_link = null

				// 服务端 苹果版本
				let ios_version = null
				// 服务端 苹果包链接
				let ios_link = null
				// console.log(res)
				//安卓
				android_version = res.android_edition
				android_link = res.android_update
				//苹果
				ios_version = res.ios_edition
				ios_link = res.ios_update


				let version = OS === 'android' ? android_version : ios_version;
				let link = OS === 'android' ? android_link : ios_link;

				//APP更新信息
				let edition = {
					version: '',
					link: '',
					currentVersion: '',
					isUpdate: ''
				}

				edition.version = version
				edition.link = link

				ctx.commit('$EDITION', edition);

				// 如果是安卓平台
				// 如果后端没有设置安卓版本号或者安装包下载地址,跳过
				if (!version || !android_link) return

				// 如果后端返回的版本包含当前版本号,跳过
				plus.runtime.getProperty(plus.runtime.appid, app => {
					//APP更新信息
					edition.currentVersion = app.version

					uni.setStorageSync('version', app.version);

					let fwq = version.replace(/\./g, '') //服务器版本号
					let bd = app.version.replace(/\./g, '') //app版本号
					let isUpdate = null //是否需要更新

					if (Number(bd) < Number(fwq)) {
						isUpdate = true
					} else {
						isUpdate = false
					}

					edition.isUpdate = isUpdate

					ctx.commit('$EDITION', edition);

					if (!isUpdate) return

					let showCancel = true //显示取消按钮

					showCancel = res.force_update == 1 ? false : true

					let text = res.app_update_content.replace(/\\n/g, '\n')

					uni.showModal({
						content: text,
						confirmText: '立即更新',
						showCancel,
						success: e => {
							if (!e.confirm) {
								//关闭安卓APP
								plus.runtime.quit();
								//关闭苹果APP
								plus.ios.import("UIApplication").sharedApplication()
									.performSelector("exit");
							} else {
								if (link.indexOf('.apk') != -1 || link.indexOf('.wgt') != -
									1) {
									uni.reLaunch({
										url: `/pages/update/update?link=${link}`
									})
								} else {
									plus.runtime.openURL(link); //使用内置浏览器打开
								}
							}
						}
					});
				})
			})
		}

	}
}
